package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    //注入dao对象
    @Autowired
    private CheckGroupDao checkGroupDao;

    //添加检查组
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组
        checkGroupDao.add(checkGroup);
        //设置检查组和检查项的多对多关联关系, 操作中间表
        Integer checkGroupId = checkGroup.getId();
        if(checkitemIds != null && checkitemIds.length>0){
            Map<String,Integer> map = new HashMap<>();
            map.put("checkgroupId",checkGroupId);
            map.put("checkitemId",checkGroupId);
            checkGroupDao.setCheckGroupAndCheckItem(map);

        }

    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        List<CheckGroup> list = checkGroupDao.findByCondition(queryString);
        PageInfo info = new PageInfo(list);
        long total = info.getTotal();
        List<CheckGroup> rows = info.getList();
        return new PageResult(total,rows);
    }

    //根据id查询检查组
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    //根据检查组id查询对应的检查项id
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> checkitemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return checkitemIds;
    }

    //编辑检查组, 同时需要更新和检查关联关系
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //根据id删除中间表数据(清理原有关联关系)
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //向中间表插入数据
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
        //更新检查组基本信息
        checkGroupDao.edit(checkGroup);
    }


    //向中间表插入数据(建立检查组和检查项关联关系)
    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        if (checkitemIds != null &&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("checkitem_id",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    //删除数据
    public void delete(Integer id) {
        //判断当前检查项是否关联检查组
        checkGroupDao.deleteAssociation(id);
        checkGroupDao.delete(id);

    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list = checkGroupDao.findAll();
        return list;
    }

}
